//
//  mmjSettingViewController.m
//  Social
//
//  Created by seirra on 2018/5/14.
//  Copyright © 2018年 mmj. All rights reserved.
//

#import "mmjSettingViewController.h"
#import <SDImageCache.h>
#import "mmjClearCacheCell.h"
#import "mmjSettingCell.h"
#import "mmjOtherCell.h"
@interface mmjSettingViewController ()

@end
static NSString * const mmjClearCacheID = @"mmjClearCacheCell";
static NSString * const mmjSettingCellID = @"mmjSettingCell";
static NSString * const mmjOtherCellId = @"mmjOtherCell";
@implementation mmjSettingViewController
-(instancetype)init{
    return [self initWithStyle:UITableViewStyleGrouped];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = mmjCommonBgColor;
    self.navigationItem.title = @"设置";
   // [self getcachesize2];//获取文件夹大小
    //注册
    [self.tableView registerClass:[mmjClearCacheCell class] forCellReuseIdentifier:mmjClearCacheID];
    
    [self.tableView registerClass:[mmjSettingCell class] forCellReuseIdentifier:mmjSettingCellID];
    [self.tableView registerClass:[mmjOtherCell class] forCellReuseIdentifier:mmjOtherCellId];
    
}
-(void)getcachesize2{
    //总大小
    NSUInteger size = 0;
    //获得缓存路径
    NSString * cachepath =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSString* dirpath = [cachepath stringByAppendingPathComponent:@"MP3"]; //拼接路径
    //文件管理者
    NSFileManager * manager = [NSFileManager defaultManager];
    
    //获得文件夹的属性
//    [manager attributesOfItemAtPath:dirpath error:nil];
    
    //获得文件夹的大小 == 获得文件夹中所有文件的大小之和
//    [manager contentsOfDirectoryAtPath:dirpath error:nil];
    
     NSArray * subpaths =  [manager subpathsAtPath:dirpath];//能获得子路径里所有内容
    
    for (NSString * subpath in subpaths) {
        //全路径
        NSString * fullsubpath = [dirpath stringByAppendingPathComponent:subpath];
        mmjLog(@"full = %@",fullsubpath);
        //文件夹属性
//        NSDictionary * attrs = [manager attributesOfItemAtPath:fullsubpath error:nil];
//        //把文件大小相加
//        size += [attrs[NSFileSize] unsignedIntegerValue];
        
        size += [manager attributesOfItemAtPath:fullsubpath error:nil].fileSize;
    }
    mmjLog(@"size = %zd",size);
    
   
}

-(void)getcachesize1{
    //总大小
    NSUInteger size = 0;
    //获得缓存路径
    NSString * cachepath =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSString* dirpath = [cachepath stringByAppendingPathComponent:@"MP3"]; //拼接路径
    //文件管理者
    NSFileManager * manager = [NSFileManager defaultManager];
    
    //获得文件夹的属性
    //    [manager attributesOfItemAtPath:dirpath error:nil];
    
    //获得文件夹的大小 == 获得文件夹中所有文件的大小之和
    //    [manager contentsOfDirectoryAtPath:dirpath error:nil];
    
    //NSArray * subpaths =  [manager subpathsAtPath:dirpath];//能获得子路径里所有内容
    NSDirectoryEnumerator * enumerator= [manager enumeratorAtPath:dirpath];
    //enumerator遍历 / 迭代
    for (NSString * subpath in enumerator) {
        //全路径
        NSString * fullsubpath = [dirpath stringByAppendingPathComponent:subpath];
        //mmjLog(@"full = %@",fullsubpath);
        //文件夹属性
        //        NSDictionary * attrs = [manager attributesOfItemAtPath:fullsubpath error:nil];
        //        //把文件大小相加
        //        size += [attrs[NSFileSize] unsignedIntegerValue];
        
        size += [manager attributesOfItemAtPath:fullsubpath error:nil].fileSize;
    }
    mmjLog(@"size = %zd",size);
    
    
}
#pragma mark 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    // 1.确定重用标示:
//    static NSString *ID = @"setting";
//
//    // 2.从缓存池中取
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//
//    // 3.如果空就手动创建
//    if (!cell) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
//    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    // 4.设置数据
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
            return [tableView dequeueReusableCellWithIdentifier:mmjClearCacheID];
        }
        else{
            mmjSettingCell* cell = [tableView dequeueReusableCellWithIdentifier:mmjSettingCellID];
            if (indexPath.row == 1) {
                cell.textLabel.text = @"检查更新";
            }else if(indexPath.row == 2){
                cell.textLabel.text = @"评分";
            }
            else if (indexPath.row == 3 ){
                cell.textLabel.text = @"推送设置";
            }
            else{
                cell.textLabel.text = @"其它";
            }
            return cell;
        }
    }
    else{
        return [tableView dequeueReusableCellWithIdentifier:mmjOtherCellId];
    }
    
   
    
    
//    cell.selectionStyle = UIAccessibilityTraitNone;
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    mmjlogfunc;
}


@end
